package phase1.part2;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * A Partition divides a set of states into two subsets: accepting and non-accepting.
 * 
 * @author devinwang91
 * @version Nov 13, 2012
 */
public class Partition{
	private List<String>accepting;
	private List<String>nonAccepting;
	
	/**
	 * Create a Partition from the set of states and a list of accepting states.
	 * @param states The set of states
	 * @param acceptingDFA The list of accepting states
	 */
	public Partition(Set<String>states, List<State>acceptingDFA){
		accepting = new ArrayList<>();
		nonAccepting = new ArrayList<>();
		
		for(State s : acceptingDFA){
			accepting.add(s.getLabel());
		}
		
		for(String s : states){
			if(!accepting.contains(s)){
				nonAccepting.add(s);
			}
		}
	}
	
	/**
	 * Get the accepting subset.
	 * @return An ArrayList of accepting states.
	 */
	public List<String> getAccepting(){
		return accepting;
	}
	
	/**
	 * Get the non-accepting subset.
	 * @return An ArrayList of non-accepting states.
	 */
	public List<String> getNonAccepting(){
		return nonAccepting;
	}
	
	@Override
	public boolean equals(Object obj){
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Partition))
			return false;
		Partition other = (Partition) obj;
		if (accepting == null) {
			if (other.getAccepting() != null)
				return false;
		}
        else if (!accepting.containsAll(other.getAccepting()))
			return false;
        else if (!other.getAccepting().containsAll(accepting))
            return false;
		if (nonAccepting == null) {
			if (other.getNonAccepting() != null)
				return false;
		}
		else if (!nonAccepting.containsAll(other.getNonAccepting()))
			return false;
		else if (!other.getNonAccepting().containsAll(nonAccepting))
			return false;
		return true;
	}
	
	@Override
	public String toString(){
		return "Accepting: " + accepting + " NonAccepting: " + nonAccepting;
	}
}